/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.unitn.science.matr155863_148346_145963.bccstore.servlet;

import it.unitn.science.matr155863_148346_145963.bccstore.DBManager;
import it.unitn.science.matr155863_148346_145963.bccstore.items.Product;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

/**
 *
 * @author berto
 */
public class ShowProductDetailsSRV extends HttpServlet {

    private DBManager db;
    private static Logger log = Logger.getLogger(ShowProductDetailsSRV.class);
    
    @Override
    public void init() throws UnavailableException{
            db=(DBManager)super.getServletContext().getAttribute("db_manager");
    }
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Product product=null;
        String address=null;
        try {
            
            if(request.getParameter("id")!=null){
                Integer id=Integer.parseInt(request.getParameter("id"));
                product=db.getProduct(id);
                if(product!=null){
                    showDetails(product,request,out);
                }else{
                    //Product not found
                    out.println("<h2>No product found</h2>");
                }
            }else{
                //No product defined
                out.println("<h2>No product defined</h2>");
            }
        }catch(Exception ex){
            address="/contents/public/errorPage.html";
        } finally {    
            if(address!=null){
                RequestDispatcher dispatcher=request.getRequestDispatcher(address);
                dispatcher.include(request, response);
            }
        }
    }
    
    private void showDetails(Product p,HttpServletRequest request,PrintWriter out ) {
        out.println("<div class=\"content\">");
        out.println("<h2>"+p.getName()+"</h2>");
        out.println("<img src=\""+request.getContextPath()+"/images/products/"+p.getImageName()+"\"/>");
        out.println("<div class=\"details\">");
        out.println("<h4>Price:</h4><p>"+p.getPrice()+" euro ;</p>");
        out.println("<h4>Quantity:</h4><p>"+p.getQuantity()+" "+p.getUmName()+" ;</p>");
        out.println("<h4>Description:</h4><p>"+p.getHumanDescription()+" ;</p>");
        out.println("<h4>Seller:</h4><p>"+p.getSellerName()+" ;</p>");
        out.println("</div>");
        out.println("</div>");
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
